﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace TRicheControl.WinformControl
{
    using TRicheControl.Comm;
    using WindowsAPI;

    [ToolboxBitmap(typeof(ListBox))]
    [Designer(typeof(ControlDesigner))]
    public partial class RicheListBox : ListBox
    {
        private Pen mClearBorderPen;

        private Graphics mGraphics;
        public Graphics Graphics
        {
            get
            {
                if (base.IsDisposed)
                {
                    return null;
                }
                if (mGraphics == null) 
                {
                    mGraphics = base.CreateGraphics();
                }
                return mGraphics;
            }
        }

        public RicheListBox()
        {
            mClearBorderPen = new Pen(SystemColors.Control);
            this.DrawMode = System.Windows.Forms.DrawMode.Normal;
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0xf || m.Msg == 0x133)//m.Msg == WM_PAINT)
            {
                IntPtr hDC = NativeMethods.GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0)
                    return;
                Graphics g = Graphics.FromHdc(hDC);
                DrawListBox(g);
                NativeMethods.ReleaseDC(m.HWnd, hDC);
            }
        }

        private void DrawListBox(Graphics g)
        {
            Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            g.DrawRectangle(mClearBorderPen, rect);
            if (this.Enabled)
            {
                GraphicsStyle.Styles.ListBoxStyle.DrawBoxEnableBorder(g, rect);
            }
            else
            {
                GraphicsStyle.Styles.ListBoxStyle.DrawBoxUnEnableBorder(g, rect);
            }
        }
    }
}
